package com.yupi.moonBI.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;
/**
 * Caffine本地缓存配置类
 * 作者博客：<a href="https://blog.csdn.net/weixin_65935065">https://blog.csdn.net/weixin_65935065</a>
 * @author 苦行修士
 */
@Configuration
@EnableCaching
public class CaffeineCacheConfig {
    /**
     * Caffeine缓存管理器
     * @return
     */
    @Bean
    public CaffeineCacheManager cacheManager() {
        // 设置Caffeine缓存，设置缓存写入后10分钟过期
        CaffeineCacheManager cacheManager = new CaffeineCacheManager("charts","chartList");
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .expireAfterWrite(10, TimeUnit.MINUTES)
                // 初始的缓存空间大小
                .initialCapacity(100)
                //最大缓存条数为100
                .maximumSize(100));

        return cacheManager;
    }
}
